//
//  WTPersonInfoManager.m
//  WTLiveSDKDemo
//
//  Created by ben on 2016/12/1.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTPersonInfoManager.h"
#import "WTUserInfoService.h"


static NSString* K_WX_CurrentLoginUserInfo = @"CurrentLoginUserInfo";//个人信息
static NSString* K_WT_Session_token = @"K_WX_Session_token";//个人信息
static NSString* K_WT_Net_Ease_Accid = @"K_WX_Net_Ease_Accid";//个人信息
static NSString* K_WT_Net_Ease_Token = @"K_WX_Net_Ease_Token";//个人信息

@interface WTPersonInfoManager ()
@property (nonatomic,strong) WTUserInfoService* userInfoService;
@end

@implementation WTPersonInfoManager

+ (instancetype)sharedManager{
    
    static WTPersonInfoManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[WTPersonInfoManager alloc] init];
        
    });
    return manager;
}

- (void)fetchPersonInfo:(WTPersonInfoFetchBlock)personInfoBlock{
    
    __weak __typeof(self)weakSelf = self;
    [self.userInfoService start:@{@"session_token":WTConfig.session_token} completionHandler:^(id responseObject, NSError *error) {
        
        if(!error){
            [weakSelf saveUserInfo:responseObject];
            personInfoBlock(YES);
        }else {
            personInfoBlock(NO);
        }
    }];
}

#pragma mark 
- (WTUserInfoService *)userInfoService{
    if(!_userInfoService){
        _userInfoService = [[WTUserInfoService alloc] init];
    }
    return _userInfoService;
}

#pragma mark
- (BOOL)isLogin{
    
    if(WTConfig.net_token == nil){
        return NO;
    }
    return YES;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        
        NSUserDefaults* store = [NSUserDefaults standardUserDefaults];
        NSDictionary*   userInfo = [store objectForKey:K_WX_CurrentLoginUserInfo];
        if(userInfo){
            WTUserInfoModel* model = [[WTUserInfoModel alloc] initWithUserInfo:userInfo];
            [self setConfigInfo:model];
        }
    }
    return self;
}

- (void)saveOauthInfo{
    
    NSUserDefaults* store = [NSUserDefaults standardUserDefaults];
    [store setObject:WTConfig.session_token forKey:K_WT_Session_token];
    [store setObject:WTConfig.net_accid forKey:K_WT_Net_Ease_Accid];
    [store setObject:WTConfig.net_token forKey:K_WT_Net_Ease_Token];
    [store synchronize];
}

- (void)saveUserInfo:(WTUserInfoModel *)userInfoModel{
    
    [self setConfigInfo:userInfoModel];
    //保存用户信息
    NSUserDefaults* store = [NSUserDefaults standardUserDefaults];
    [store setObject:[userInfoModel userInfoJson] forKey:K_WX_CurrentLoginUserInfo];
    [store synchronize];
}

- (void)setConfigInfo:(WTUserInfoModel*)userInfoModel{
    
    WTConfig.nick = userInfoModel.userPrivateInfoModel.privateinfo_name;
    WTConfig.level = [userInfoModel.userRelationInfoModel.relationinfo_level integerValue];
    WTConfig.avatar = userInfoModel.userPrivateInfoModel.privateinfo_avatar;
    WTConfig.user_id = userInfoModel.userPrivateInfoModel.privateinfo_id;
    WTConfig.cuteCode = userInfoModel.userPrivateInfoModel.privateinfo_cutecode;
    WTConfig.sex = userInfoModel.userPrivateInfoModel.privateinfo_sex;
    WTConfig.signature = userInfoModel.userPrivateInfoModel.privateinfo_signature;
    WTConfig.address = userInfoModel.userPrivateInfoModel.privateinfo_address;
    WTConfig.mobile = [userInfoModel.userPrivateInfoModel.privateinfo_mobile integerValue];
    WTConfig.mobile_auth = userInfoModel.userPrivateInfoModel.privateinfo_mobile_auth ;
    WTConfig.anchor_push = userInfoModel.userPrivateInfoModel.privateinfo_anchor_push ;
    
}
@end
